<?php
class m_user extends Model{
    var $table = "user";
    
    function getCountRows(){
        return $this->db->count_all($this->table);
    }
    function getListUser($offset = NULL,$limit = NULL){
        $query = $this->db->query("select u.*,m.nama as nama from user u join member m on u.id_member = m.id limit $offset,$limit");
        if($query->num_rows()>0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getUserById($id = NULL){
        if($id != NULL){
            $where = "where u.id='$id'";
        }else $where = "";
        $query = $this->db->query("select u.*,m.nama as nama from user u join member m on u.id_member = m.id $where");
        if($query->num_rows()>0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function addUser($data){
        $insert = $this->db->insert($this->table,$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editUser($data){
        $this->db->where('id_member',$data['id_member']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function cekUsername($username,$idMember){
        $cek = $this->db->query("select username from user where username='$username' and id_member != '$idMember'");
        if($cek->num_rows()>0){
            return false;
        }else return true;
    }
    function aktivasi($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function editPrivilege($data){
//        $mod = "1-";
        $mod = "";
        $akses = "";
        $i = 0;
        foreach ($data['hak'] as $hak){
            $i++;
            $akses .= "$hak-"; 
            $qmod = $this->db->query("select id_module from sub_module where id = '$hak'");
            if($qmod->num_rows() > 0){
                $dmod = $qmod->result_array();
                $nomod[$i] = $dmod[0]['id_module'];
            }
        }
        $nomod = array_unique($nomod);
        foreach ($nomod as $module){
            $mod .= "$module-";
        }
        $akses = substr($akses, 0, -1);
        $mod = substr($mod, 0, -1);
        $dataUpdate = array('hak_module' => $mod,
            'hak_sub' => $akses);
        
        $this->db->where('id',$data['idUser']);
        $update = $this->db->update($this->table,$dataUpdate);
        
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function getPrivilege($id){
        $query = $this->db->query("select hak_module,hak_sub from user where id='$id' and status='1'");
        if($query->num_rows()>0){
            return $query->result_array();
        }
    }
    function ubahPassword($data){
        $dataUser = $this->getUserById($data['id']);
        foreach ($dataUser as $row);
        if($row['password'] != md5($data['oldpass'])){
            return false;
        }else{
            $this->db->where('id',$row['id']);
            $update = $this->db->update($this->table,array('password' => md5($data['newpass'])));
            if($update){
                return TRUE;
            }else return FALSE;
        }
    }
    function resetPassword($id){
        $this->db->where('id',$id);
        $reset = $this->db->update($this->table,array('password'=>md5('1234')));
        if($reset){
            return TRUE;
        }else return FALSE;
    }
}
?>
